#include "mpHttpRequestMessage.h"

#include "Internal/mpHttpRequestEmptyContent.h"
#include "Internal/mpHttpRequestFileContent.h"
#include "Internal/mpHttpRequestMultipartContentImpl.h"
#include "Internal/mpHttpRequestStringContent.h"
#include "Internal/mpHttpRequestMessageImpl.h"

using namespace MP::HTTP::INTERNAL;

namespace MP{
namespace HTTP{

/*****************************************
 * HttpRequestContent
 *****************************************/

//设置ContentType
void HttpRequestContent::setContentType(const std::string & content_type) {
    _httpcontent_type = content_type;
}
//获取ContentType
const std::string& HttpRequestContent::getContentType() const{
    return _httpcontent_type;
}

/****************************************************
 * HttpRequestMessage && HttpRequestContent Creater
 ***************************************************/

//创建空的HttpRequestContent
HttpRequestContent::ptr_s createHttpRequestEmptyContent(){
    return std::make_shared<HttpRequestEmptyContent>();
}

//创建内容为字符串的HttpRequestContent
//HttpRequestContent::ptr_s createHttpRequestStringContent(const std::string& data){
//    return createHttpRequestStringContent(data,"text/plain");
//}

HttpRequestContent::ptr_s createHttpRequestJsonContent(const std::string& json){
    return createHttpRequestStringContent(json,"application/json");
}

HttpRequestContent::ptr_s createHttpRequestXmlContent(const std::string& xml){
    return createHttpRequestStringContent(xml,"application/xml");
}

HttpRequestContent::ptr_s createHttpRequestFormContent(const std::string& form){
    //Content-Type被指定为 application/x-www-form-urlencoded;charset=utf-8；
    //其次，提交的表单数据会转换为键值对并按照 key1=val1&key2=val2 的方式进行编码，key 和 val 都进行了 URL 转码

    return createHttpRequestStringContent(form,"application/x-www-form-urlencoded;charset=utf-8");
}

HttpRequestContent::ptr_s createHttpRequestStringContent(const std::string& data,
                                                         const std::string & httpcontent_type){
    return std::make_shared<HttpRequestStringContent>(data,httpcontent_type);
}

//创建内容为文件的HttpRequestContent
HttpRequestContent::ptr_s createHttpRequestFileContent(const std::string& filePath){
    return createHttpRequestFileContent(filePath,"application/octet-stream");
}

HttpRequestContent::ptr_s createHttpRequestFileContent(const std::string& filePath,
                                                       const std::string & httpcontent_type){
    return std::make_shared<HttpRequestFileContent>(filePath,0,0,httpcontent_type);
}

HttpRequestContent::ptr_s createHttpRequestFileContent(const std::string& filePath,
                                                       std::size_t fileOffset,
                                                       std::size_t sendContentSize){
    return createHttpRequestFileContent(filePath,fileOffset,sendContentSize,"application/octet-stream");
}

HttpRequestContent::ptr_s createHttpRequestFileContent(const std::string& filePath,
                                                       std::size_t fileOffset,
                                                       std::size_t sendContentSize,
                                                       const std::string & httpcontent_type){
    return std::make_shared<HttpRequestFileContent>(filePath,fileOffset,sendContentSize,httpcontent_type);
}

//创建内容为Form表单的HttpRequestContent
HttpRequestMultipartContent::ptr_s createHttpRequestMultipartContent(){
    return createHttpRequestMultipartContent("----WebKitFormBoundary7TMYhSONfkAM2z3a");
}

HttpRequestMultipartContent::ptr_s createHttpRequestMultipartContent(const std::string& boundary){
    return std::make_shared<HttpRequestMultipartContentImpl>(boundary);
}

//创建HttpRequestMessage
HttpRequestMessage::ptr_s createHttpRequestMessage(const std::string & uri,
                                                   MP::HTTP::HTTP_METHOD httpMethod){
    return std::make_shared<HttpRequestMessageImpl>(uri,httpMethod);
}

HttpRequestMessage::ptr_s createHttpRequestMessage(const Url& uri,
                                                   MP::HTTP::HTTP_METHOD httpMethod){
    return std::make_shared<HttpRequestMessageImpl>(uri,httpMethod);
}

HttpRequestMessage::ptr_s createHttpRequestMessage(const Url& baseAddress,
                                                   const std::string & relativeUri,
                                                   MP::HTTP::HTTP_METHOD httpMethod){
    return std::make_shared<HttpRequestMessageImpl>(baseAddress,relativeUri,httpMethod);
}


}   //HTTP
}   //MP

